new Vue({
  el: '#container',
  components: {
    'login': httpVueLoader('login.vue')//加载需要使用的vue文件
  },      
  data: {
    qiniu_url: VUE_APP_SERVER_QINIU_URL,
    coursePubId: 0,
    order: {
      orderNo: '',
      coursePubName: '',
      startTime: '',
      endTime: '',
      initialPrice: 0,
      price: 0
    },
	orderNo: 0,
	course: {
		
	}
  },
  computed: {
    validTime: function() {
      let startTime = this.order.startTime
      let endTime = this.order.endTime
      if (!startTime || !endTime) {
        return ''
      }
      return (
        '课程有效期：' +
        startTime.substring(0, 10) +
        ' - ' +
        endTime.substring(0, 10)
      )
    }
  },
  methods: {
    createQRCode(text) {
      new QRCode('qrcode', {
        text: text,
        width: 148,
        height: 148,
        colorDark: '#000000',
        colorLight: '#ffffff',
        correctLevel: QRCode.CorrectLevel.H
      })
    },
    searchOrder() {
      let interval = setInterval(() => {
        search_order(this.orderNo).then(data => {
          if (data.status === 600002) {
            clearInterval(interval)
            this.goToLearingArticle()
          }
        })
      }, 5000)
    },
    goToLearingArticle() {
      location.href = `/pages/${this.coursePubId}.html`
    },
	formatPrice(val) {
		console.info(val)
        if(typeof val === 'string'){
            if(isNaN(val)){
                return null;
            }
            // 价格转为整数
            const index = val.lastIndexOf(".");
            let p = "";
            if(index < 0){
                // 无小数
                p = val + "00";
            }else if(index === p.length - 2){
                // 1位小数
                p = val.replace("\.","") + "0";
            }else{
                // 2位小数
                p = val.replace("\.","")
            }
            return parseInt(p);
        }else if(typeof val === 'number'){
            if(val == null){
                return null;
            }
            const s = val + '';
            if(s.length === 0){
                return "0.00";
            }
            if(s.length === 1){
                return "0.0" + val;
            }
            if(s.length === 2){
                return "0." + val;
            }
            const i = s.indexOf(".");
            if(i < 0){
                return s.substring(0, s.length - 2) + "." + s.substring(s.length-2)
            }
            const num = s.substring(0,i) + s.substring(i+1);
            if(i === 1){
                // 1位整数
                return "0.0" + num;
            }
            if(i === 2){
                return "0." + num;
            }
            if( i > 2){
                return num.substring(0,i-2) + "." + num.substring(i-2)
            }
        }
    }
  },
  mounted() {
	//this.createQRCode("xxx");
    this.coursePubId = parseInt(localURL.searchParams.get('coursePubId'), 10)
	search_course_index(this.coursePubId).then(data=>{
		this.course = data;
	})
    create_order(this.coursePubId).then(data => {
	  this.orderNo = data;
      create_pay(data).then(dataUrl => {
        // TODO: 判断status: 1
		console.log(dataUrl)
        this.createQRCode(dataUrl)
        this.searchOrder()
      })
    })
  }
})
